﻿using System;
namespace Kis.Xroad.Eu {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="ReferencedComplexTypeArray", Namespace="http://kis.x-road.eu/", Target=typeof(ReferencedComplexTypeArray))]
    public class ReferencedComplexTypeArraySerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected ReferencedComplexTypeArray clazz;
        
        public ReferencedComplexTypeArraySerializer(ReferencedComplexTypeArray clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("Employees");
            if (clazz.Employees!=null) {
                for (int x = 0; ((clazz.Employees != null) 
                            && (x < clazz.Employees.Count)); x = (x + 1)) {
                    if (clazz.Employees[x]!=null) {
                        Writer.WriteStartElement("item");
                        global::Xtee.Core.SerializationMetadata metaEmployees = this.GetMetaData(clazz.Employees[x]);
                        if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Employee), clazz.Employees[x].GetType())) {
                            Writer.WriteTypeAttribute(metaEmployees.TypeQualifiedName.Namespace, metaEmployees.TypeQualifiedName.Name);
                        }
                        var employeesSerializer = metaEmployees.NewSerializer(clazz.Employees[x]);
                        employeesSerializer.WriteXmlAttributes(Writer);
                        employeesSerializer.WriteXml(Writer);
                        Writer.WriteEndElement();
                    }
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Employees");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="Employees") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ReferencedComplexTypeArray>Employees");
                }
                clazz.Employees = new System.Collections.Generic.List<global::Kis.Xroad.Eu.Employee>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("Employees");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if ((Reader.IsElementNil() == false)) {
                            global::Xtee.Core.SerializationMetadata metaEmployees = this.GetMetaData<global::Kis.Xroad.Eu.Employee>(Reader, "Employee", "http://kis.x-road.eu/");
                            global::Kis.Xroad.Eu.Employee item = metaEmployees.NewEntity<global::Kis.Xroad.Eu.Employee>();
                            clazz.Employees.Add(item);
                            if ((Reader.IsEmptyElement == false)) {
                                var employeesSerializer = metaEmployees.NewSerializer(item);
                                employeesSerializer.ReadXmlAttributes(Reader);
                                Reader.ReadStartElement("item");
                                employeesSerializer.ReadXml(Reader);
                                Reader.ReadEndElement();
                            }
                            else {
                                Reader.Skip();
                            }
                        }
                        else {
                            clazz.Employees.Add(null);
                            Reader.Skip();
                        }
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ReferencedComplexTypeArray>Employees", Reader.LocalName);
            }
        }
    }
}
